package model;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import util.MD5Code;

/**
 * A data access object (DAO) providing persistence and search support for
 * Teacher entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see model.Teacher
 * @author MyEclipse Persistence Tools
 */

public class TeacherDAO extends HibernateDaoSupport {
	private static final Log log = LogFactory.getLog(TeacherDAO.class);
	// property constants
	public static final String TEACHERUSERNAME = "teacherusername";
	public static final String TEACHERNAME = "teachername";
	public static final String TEACHERSEX = "teachersex";
	public static final String TEACHEREMAIL = "teacheremail";
	public static final String TEACHERTELEPHONE = "teachertelephone";
	public static final String TEACHERPOSITION = "teacherposition";
	public static final String TEACHERADDRESS = "teacheraddress";
	public static final String TEACHERPASSWORD = "teacherpassword";

	protected void initDao() {
		// do nothing
	}

	/**
	 * 验证登陆
	 * @param teacherusername
	 * @param teacherpassword
	 * @return
	 */
	public String loginValidate(String teacherusername,String teacherpassword)
	{
		MD5Code m = new MD5Code();
		String hql="FROM Teacher AS t WHERE t.teacherusername=:teacherusername";
		Query q=super.getSession().createQuery(hql);
		q.setString("teacherusername", teacherusername);
		if(q.list().size()==0)
			return "UserNotExist";
		
		String hql1="FROM Teacher AS t WHERE t.teacherusername=:teacherusername and t.teacherpassword=:teacherpassword";
		Query q1=super.getSession().createQuery(hql1);
		q1.setString("teacherusername", teacherusername);
		q1.setString("teacherpassword", m.getMD5ofStr(teacherpassword));
		if(q1.list().size()==0)
			return "PasswordError";
		Teacher t = new Teacher();
		t =(Teacher) q1.list().get(0);
		return t.getId()+";"+t.getTeachername();
	}
	
	/**
	 * 修改密码
	 *
	 */
	public void editPassword(String teacherusername,String teacherpasswordnew)
	{
		MD5Code m = new MD5Code();
		String hql="FROM Teacher AS t WHERE t.teacherusername=:teacherusername";
		Query q=super.getSession().createQuery(hql);
		q.setString("teacherusername", teacherusername);
		Teacher t = new Teacher();
		t =(Teacher) q.list().get(0);
		int id=t.getId();
		System.out.println("该用户的id为："+id);
		System.out.println("该用户的id为："+teacherpasswordnew);
		String hql1="UPDATE Teacher AS t SET t.teacherpassword=:teacherpasswordnew WHERE t.id=:id";
		Query q1=super.getSession().createQuery(hql1);
		q1.setInteger("id", id);
		q1.setString("teacherpasswordnew", m.getMD5ofStr(teacherpasswordnew));
		q1.executeUpdate();
	}
	
	public List getTeacherById(String id)
	{
		String hql="FROM Teacher AS t";
		String temp="WHERE ";
		String clause=temp;
		
		if(id!=null&&!"".equals(id))
		{
			clause=clause+"t.id=:id";
		}
		if(!clause.equals(temp))
		{
			hql=hql+" "+clause;
		}
		System.out.println(hql);
		Query q=super.getSession().createQuery(hql);
		if(id!=null&&!"".equals(id))
		{
			q.setString("id",id);
		}
		System.out.println(hql);
		List searchlist=q.list();
		return searchlist;
	}
	
	
	/**
	 * delete all teacher info,for import the newest teacher info
	 * @param teachername
	 * @param teachersex
	 * @return
	 */
	public void deleteAllTeacherInfo()
	{
		String hql="DELETE FROM Teacher AS t";
		Query q=super.getSession().createQuery(hql);
		q.executeUpdate();
	}
	
	//按姓名和性别搜索老师信息
	public List searchTeachers(String teachername,String teachersex)
	{
		String hql="FROM Teacher AS t";
		String temp="WHERE ";
		String clause=temp;
		
		if(teachername!=null&&!"".equals(teachername))
		{
			clause=clause+"t.teachername like :teachername";
		}
		if(!clause.equals(temp)&&teachersex!=null&&!"".equals(teachersex))
		{
			clause=clause+" and t.teachersex like :teachersex";
		}
		if(clause.equals(temp)&&teachersex!=null&&!"".equals(teachersex))
		{
			clause=clause+"t.teachersex like :teachersex";
		}
		
		if(!clause.equals(temp))
		{
			hql=hql+" "+clause;
		}
		System.out.println(hql);
		Query q=super.getSession().createQuery(hql);
		if(teachername!=null&&!"".equals(teachername))
		{
			q.setString("teachername",'%'+teachername+'%');
		}
		if(teachersex!=null&&!"".equals(teachersex))
		{
			q.setString("teachersex",'%'+teachersex+'%');
		}
		System.out.println(hql);
		List searchlist=q.list();
		return searchlist;
	}
	
	public void save(Teacher transientInstance) {
		log.debug("saving Teacher instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Teacher persistentInstance) {
		log.debug("deleting Teacher instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Teacher findById(java.lang.Integer id) {
		log.debug("getting Teacher instance with id: " + id);
		try {
			Teacher instance = (Teacher) getHibernateTemplate().get(
					"model.Teacher", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Teacher instance) {
		log.debug("finding Teacher instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Teacher instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Teacher as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByTeacherusername(Object teacherusername) {
		return findByProperty(TEACHERUSERNAME, teacherusername);
	}

	public List findByTeachername(Object teachername) {
		return findByProperty(TEACHERNAME, teachername);
	}

	public List findByTeachersex(Object teachersex) {
		return findByProperty(TEACHERSEX, teachersex);
	}

	public List findByTeacheremail(Object teacheremail) {
		return findByProperty(TEACHEREMAIL, teacheremail);
	}

	public List findByTeachertelephone(Object teachertelephone) {
		return findByProperty(TEACHERTELEPHONE, teachertelephone);
	}

	public List findByTeacherposition(Object teacherposition) {
		return findByProperty(TEACHERPOSITION, teacherposition);
	}

	public List findByTeacheraddress(Object teacheraddress) {
		return findByProperty(TEACHERADDRESS, teacheraddress);
	}

	public List findByTeacherpassword(Object teacherpassword) {
		return findByProperty(TEACHERPASSWORD, teacherpassword);
	}

	public List findAll() {
		log.debug("finding all Teacher instances");
		try {
			String queryString = "from Teacher";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Teacher merge(Teacher detachedInstance) {
		log.debug("merging Teacher instance");
		try {
			Teacher result = (Teacher) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Teacher instance) {
		log.debug("attaching dirty Teacher instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Teacher instance) {
		log.debug("attaching clean Teacher instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static TeacherDAO getFromApplicationContext(ApplicationContext ctx) {
		return (TeacherDAO) ctx.getBean("TeacherDAO");
	}
}